<script lang="ts" setup>
import { Component, RendererElement, RendererNode, VNode, computed } from 'vue'
import { Handle } from '@vue-flow/core'
import { ArgumentItem } from '@/types/argument'
import { NIcon, NButton } from 'naive-ui'
import { DocumentArrowUp16Regular } from '@vicons/fluent'
import { open } from '@tauri-apps/plugin-dialog'

interface Props {
  label: string | object | VNode<RendererNode, RendererElement, {
    [key: string]: Any;
  }> | Component | undefined,
  id: string,
  outputs: (ArgumentItem & { active: boolean, value: Any })[]
}
const props = defineProps<Props>()
const emit = defineEmits<{
  'update:value': [Any]
}>()

const output = computed(() => ({
  id: props.outputs[0].name,
  position: 'right',
  type: 'source',
  label: props.outputs[0].name,
  offset: 0,
  active: props.outputs[0].active,
  data: props.outputs[0].value
} as Any))

async function onSelect () {
  const res = await open({
    multiple: false,
    directory: false,
    filters: [
      {
        name: '图片',
        extensions: ['png', 'jpeg', 'jpg']
      }
    ]
  })
  if (!res) {
    return
  }
  emit('update:value', res.path)
}

defineExpose({
  get code () {
    return `def fn_${props.id}():\n\treturn "${output.value.data}"`
  },
  run (_: Any) {
    return {
      url: {
        type: 'string',
        value: output.value.data
      }
    }
  }
})
</script>

<template>
  <div class="flow-node">
    <div class="header">
      <span class="label">{{ label }}</span>
    </div>
    <div class="workspace" :style="{ '--lines': 2 }">
      <n-button size="small" text type="info" @click.stop.prevent="onSelect">
        <n-icon><DocumentArrowUp16Regular /></n-icon>选择文件
      </n-button>
    </div>
    <!-- 输出参数 -->
    <Handle :id="output.id" :position="output.position" :connectable-end="false"
      :style="{ '--offset': output.offset }" :class="{active: output.active}">
      <span class="handle-label" :class="{active: !!output.data}">
        {{ output.label }}
        <div class="data-viewer" v-if="output.data">{{ output.data }}</div>
      </span>
    </Handle>
  </div>
</template>

<style lang="less" scoped>
.flow-node {
  padding: 0;
  --border-color: #445961;
  border: 1px solid var(--border-color);
  border-top-color: #445961;
  font-size: 12px;
  background: #303c3f;
  border-bottom-left-radius: 3px;
  border-bottom-right-radius: 3px;

  .header {
    background: #303c3f;
    padding: 5px 8px;
    display: flex;
    align-items: center;
    border-top: 1px solid var(--border-color);
    border-left: 1px solid var(--border-color);
    border-right: 1px solid var(--border-color);
    border-top-left-radius: 3px;
    border-top-right-radius: 3px;
    position: absolute;
    bottom: 100%;
    left: 0;
    right: 0;

    .label {
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
    }
  }

  .workspace {
    min-width: 120px;
    min-height: calc(var(--lines) * 18px);
    display: flex;
    align-items: center;
    .n-button {
      margin-left: 10px;
      .n-icon {
        margin-right: 5px;
      }
    }
  }
}

.vue-flow__handle {
  transition: all 0.175s 0.25s;
  width: 4px;
  height: 4px;
  top: 18px;
  display: flex;
  align-items: center;

  .handle-label {
    position: absolute;
    white-space: nowrap;
    .data-viewer {
      position: absolute;
      opacity: 0;
      pointer-events: none;
      max-width: 320px;
      max-height: 480px;
      overflow-x: hidden;
      overflow-y: overlay;
      white-space: wrap;
      background: #252d2f;
      border: 1px solid #445961;
      padding: 5px 8px;
      border-radius: 3px;
      color: unset;
    }
    &:hover {
      .data-viewer {
        opacity: 1;
        pointer-events: all;
      }
    }
  }

  &.vue-flow__handle-right {
    transform: translate(50%, calc(18px * var(--offset) - 50%));

    .handle-label {
      right: 12px;
      &.active {
        color: #3FF;
      }
    }

    &.active {
      background-color: #3FF;
    }
  }

  &:hover {
    transition: all 0.175s 0s;
    border-color: var(--el-border-color);
    background-color: var(--el-color-primary);
  }
}
</style>
<style>
.vue-flow__node.vue-flow__node-script.selected .flow-node {
  --border-color: #63e2b7;
}
</style>
